package com.kye.express;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.multidex.MultiDex;
import android.support.v7.app.AppCompatDelegate;

import com.baidu.mapapi.SDKInitializer;
import com.f2prateek.rx.preferences2.RxSharedPreferences;
import com.kye.base.Logger;
import com.kye.express.utils.ActivityInitUtil;
import com.kye.express.utils.RefreshLoadViewUtils;
import com.kye.express.utils.XiaoNengUtils;
import com.kye.model.ApiService;
import com.kye.model.SharedPreferencesManager;
import com.kye.model.base.RetrofitManager;

import cn.jpush.android.api.JPushInterface;

public class App extends Application {

    /*
     * 在5.0以下的设备上使用SVG
     */
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    private static App instance;
    public static App getInstance() {
        return instance;
    }

    private static Context context;
    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (instance == null) {
            instance = this;
        }

        context = this;

        ActivityInitUtil.init(getInstance());

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        RxSharedPreferences rxSharedPreferences = RxSharedPreferences.create(sharedPreferences);
        SharedPreferencesManager.setSharedPreferences(sharedPreferences);
        SharedPreferencesManager.setRxSharedPreferences(rxSharedPreferences);

        Logger.init();
        ApiService.init(this);
        RetrofitManager.init(this);
        RefreshLoadViewUtils.init();

        XiaoNengUtils.initSDK(this);
        SDKInitializer.initialize(getApplicationContext());

        //Jpush
        JPushInterface.setDebugMode(false);
        JPushInterface.init(this);
    }

    /**
     * 解决Android 5.0以下出现 java.lang.NoClassDefFoundError找不到类
     * <p>
     * source http://blog.csdn.net/cgq1030699125/article/details/72123793
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
}